<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/5/30
 * Time: 14:36
 */
namespace app\admin\controller;

use app\common\Common;
use app\data\AdminData;
use app\data\ShopData;
use app\data\UserData;
use app\data\WithdrawData;
use think\Db;

class Index extends Base
{

    /**
     * 排序方法
     * @var array
     */
    private $sortType = ['desc', 'asc'];

    /**
     * 商户排序字段
     * @var array
     */
    private $shopSort = ['create_time'];

    public function _initialize($str = null)
    {
        parent::_initialize($str); // TODO: Change the autogenerated stub
    }


    public function index()
    {

    }


    /**
     * 显示商户
     * @param $verify_status
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return \think\response\Json
     */
    public function showShop($verify_status, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'])
    {

        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->shopSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showShop($page, $pre, $sort, $verify_status),
                'total' => $shop->total('verify_status=' . $verify_status)
            ]
        ]);
    }

    /**
     * 提现申请显示
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @param int $status
     * @param $order_num
     * @return \think\response\Json
     */
    public function withdrawShow($page = 1, $pre = 15, array $sort = ['create_time' => 'desc'],
                                 $status = 0, $order_num = 0)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->shopSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $time = time();
        $time = strtotime(date('Y-m-d', $time - 24 * 60 * 60 * 7));//7天前的
        switch ($status) {
            case 0://所有的
                $where = '';
                break;
            case 1://可打款的
                $where = "a.create_time <= {$time} and a.review_status = 0";//7天前的可以打款
                break;
            case 2://不能打款的
                $where = "a.create_time > {$time} and a.review_status = 0";//7天后的不能打款
                break;
            case 3://驳回的
                $where = " a.review_status = 2";
                break;
            case 4://打款完成的
                $where = " a.review_status = 1";
                break;
            default:
                return json(['code' => $this->f_code, 'msg' => '状态错误']);
        }
        $user = new WithdrawData();
        if ($order_num != 0) {
            $where = $where . " and a.order_num={$order_num}";
        }
        $res = $user->withdrawShow($page, $pre, $sort, $where);

        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $res,
                'total' => $user->withdrawTotal($where)
            ]
        ]);
    }

    /**
     * 进行审核
     * @param $order_num
     * @param $status
     * @param string $msg
     * @return \think\response\Json
     */
    public function withdrawReview($order_num, $status, $msg = '审核通过')
    {
        if ($status != 1 && $status != 2) {
            return json(['code' => $this->f_code, 'msg' => '审核状态错误']);
        }
        if (Common::checkTableFields('drh_withdraw', 'order_num', $order_num) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在改提现请求']);
        }
        if (strlen($msg) > 200) {
            return json(['code' => $this->f_code, 'msg' => '驳回信息过长 请限制在200字内']);
        }
        $time = time();
        $user = new UserData();
        $time_start = strtotime(date('Y-m-d', $time - 24 * 60 * 60 * 6));//7天前的
        try {
            if ($status == 2) {//审核失败  数据要回滚
                $this->rollBackWithdraw($order_num, $time);
            }
            $res = $user->withdrawReview($order_num, $status, $msg, $time, $time_start);//提现完结

            if ($res == $this->f_code) {
                Db::rollback();
                return json(['code' => $this->f_code, 'msg' => '非法操作']);
            }
            return json(['code' => $this->t_code, 'msg' => 'OK']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 回滚金额
     * @param $order_num
     * @param $time
     */
    private function rollBackWithdraw($order_num, $time)
    {

        $user = new UserData();
        $user_info = $user->getWithdrawUserInfo($order_num);
        $user_integral = $user_info['user_integral'];//用户初始积分
        $user_money = $user_info['user_money'];//用户初始金钱 f
        $money = $user_info['total_money'];//退回的金额
        $fees = 0;
        $user->addUserFlow($user_info['user_id'], $user_integral, $user_money, $time, '+' . $money, 0,
            $order_num, 3, $user_money - $money, $user_integral, $fees);
        $user->changIntegralMoney($user_info['user_id'], $user_integral, $user_money + $money);
    }

    public function shopReviewChange($shop_id, $status)
    {
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在商户']);
        }
        if ($status == 1 || $status == 2) {
            $update = ['verify_status' => $status];
        } else if ($status == 3 || $status == 4) {

            $update = ["recover_status" => $status - 3];
        } else {
            return json(['code' => $this->f_code, 'msg' => '非法操作']);
        }
        $data = new AdminData();
        $res = $data->shopReviewChange($update, $shop_id);
        if ($res) {
            return json(['code' => $this->t_code, 'msg' => '修改成功']);
        } else {
            return json(['code' => $this->f_code, 'msg' => '修改失败请稍后在试']);
        }

    }

}